package com.ourteam.frame.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.web.AbstractSearchHelper;
import com.ourteam.frame.web.AbstractSearchModel;
import com.ourteam.frame.web.GridBean;

import net.dao.BaseQueryBean;

public abstract class AbstractQueryServiceController<Q extends BaseQueryBean> extends AbstractServiceController {

	@RequestMapping("/query")
	@ResponseBody
	public GridBean query(final @RequestParam Map<String, Object> queryParams, HttpServletRequest request, Model model)
			throws Exception {

		AbstractSearchModel<Q> searchModel = newSearchModel();
		
		BeanUtils.populate(searchModel, queryParams);

		AbstractSearchHelper<Q, AbstractSearchModel<Q>> searchHelper = getDataAccessHelper(searchModel);

		return searchHelper.doSearch(request);

	}

	abstract protected AbstractSearchHelper<Q, AbstractSearchModel<Q>> getDataAccessHelper(
			AbstractSearchModel<Q> searchModel);

	abstract protected AbstractSearchModel<Q> newSearchModel();
}
